package com.wangxiaoxuan.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:rabbitMQ配置文件
 * 使用topic模式
 * @Author：dongRuiLong
 * @Date 创建时间： 2022/1/28 23:22
 **/
@Configuration
public class RabbitMQConfig {

    /**
     * 消息队列名称
     */
    private static final String QUEUE_ORDER="queue_seckill_order";
    private static final String QUEUE_PAY="queue_seckill_pay";
    private static final String QUEUE_STOCK="queue_seckill_stock";
    private static final String QUEUE_DELAY="queue_seckill_delay";
    private static final String QUEUE_WX="queue_seckill_wx";

    /**
     * 交换机名称
     */
    private static final String EXCHANGE="exchange_seckill";
    private static final String EXCHANGE_WX="exchange_seckill_wx";

    /**
     * 延迟队列路由
     */
    private static final String ROUTING_KEY_DELAY="routing_key_delay";

    /**
     * 支付订单
     * @return
     */
    @Bean
    public Queue queue_pay(){
        return new Queue(QUEUE_PAY);
    }




    /**
     * 获取订单消息队列
     * @return
     */
    @Bean
    public Queue queue_order(){
        return new Queue(QUEUE_ORDER);
    }


    /**
     * 延迟队列
     * @return
     */
    @Bean
    public Queue queue_delay(){
        return QueueBuilder.durable(QUEUE_DELAY)
                //指定死信交换机，如果消息过时，消息就会投放到死信交换机
                .withArgument("x-dead-letter-exchange",EXCHANGE_WX)
                //指定死信路由，如果消息过时，消息就会根据路由投放到对应的死信队列
                .withArgument("x-dead-letter-routing-key",ROUTING_KEY_DELAY).build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue queue_dlx(){
        return QueueBuilder.durable(QUEUE_WX).build();
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public Exchange exchange_dlx(){
        return ExchangeBuilder.directExchange(EXCHANGE_WX).build();
    }

    /**
     * 绑定死信交换机和死信队列
     * @return
     */
    @Bean
    public Binding binding_dlx(){
        return BindingBuilder.bind(queue_dlx()).to(exchange_dlx()).with(ROUTING_KEY_DELAY).noargs();
    }

    /**
     * 获取库存消息队列
     * @return
     */
    @Bean
    public Queue queue_stock(){
        return new Queue(QUEUE_STOCK);
    }

    /**
     * 使用topic模式，故使用topic交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(EXCHANGE);
    }

    /**
     * 交换机绑定订单消息队列和路由
     * @return
     */
    @Bean
    public Binding binding_order(){
        return BindingBuilder.bind(queue_order()).to(topicExchange()).with("seckill.order.#");
    }

    /**
     * 交换机绑定库存消息队列和路由
     * @return
     */
    @Bean
    public Binding binding_stock(){
        return BindingBuilder.bind(queue_stock()).to(topicExchange()).with("seckill.stock.#");
    }


}
